package org.urbansoft.icl.record;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.urbansoft.icl.field.Field;
import org.urbansoft.icl.field.GenericAField;
import org.urbansoft.icl.field.GenericANField;
import org.urbansoft.icl.field.GenericANSField;
import org.urbansoft.icl.field.GenericBlankField;
import org.urbansoft.icl.field.GenericNField;
import org.urbansoft.icl.field.Position;
import org.urbansoft.icl.field.Usage;
import org.urbansoft.icl.field.ValidationCriteria;
import org.urbansoft.icl.field.validation.CashLetterDocumentationTypeValidator;
import org.urbansoft.icl.field.validation.CashLetterRecordTypeValidator;
import org.urbansoft.icl.field.validation.CollectionTypeIndicatorValidator;
import org.urbansoft.icl.field.validation.RecordTypeValidator;
import org.urbansoft.icl.field.validation.RoutingNumberValidator;
import org.urbansoft.icl.util.EasternStandardTime;

/**
 * The Class FileHeaderRecord.
 */
public class CashLetterHeaderRecord extends Record {
	public Field recordType;
	public Field collectionTypeIndicator;
	public Field destinationRoutingNumber;
	public Field eceInstitutionRoutingNumber;
	public Field cashLetterBusinessDate;
	public Field cashLetterCreationDate;
	public Field cashLetterCreationTime;
	public Field cashLetterRecordTypeIndicator;
	public Field cashLetterDocumentationTypeIndicator;
	public Field cashLetterID;
	public Field originatorContactName;
	public Field originatorContactPhoneNumber;
	public Field fedWorkType;
	public Field returnInd;
	public Field userField;
	public Field reserved;
	public Field reservedPS;

	private static Logger log = Logger.getLogger(CashLetterHeaderRecord.class);

	/**
	 * Constructor por defecto X9 USA, 80 de largo
	 */
	public CashLetterHeaderRecord() {
		fields = new ArrayList<Field>();
		recordName = "Cash Letter Header Record";
		recordTypeNumber = "10";
		initFields();
	}

	/**
	 * Constructor para lectura y edición de X9
	 * 
	 * @param read
	 *            "0" por defecto
	 */
	public CashLetterHeaderRecord(String read) {
		// log.info("New CashLetterHeaderRecord for READ");
		fields = new ArrayList<Field>();
		recordName = "Cash Letter Header Record";
		recordTypeNumber = "10";
		initFields2();
	}

	/**
	 * Constructor para creación de CashLetter en edición de un X9
	 * 
	 * @param write
	 *            0 por defecto
	 */
	public CashLetterHeaderRecord(int write) {
		// log.info("New CashLetterHeaderRecord for WRITE");
		fields = new ArrayList<Field>();
		recordName = "Cash Letter Header Record";
		recordTypeNumber = "10";
		initFields3();
	}

	/**
	 * Inits the fields.
	 */
	private void initFields2() {
		try {
			/* cash header record */
			recordType = new GenericANField(new Position(1, 2), "Record Type");
			recordType.setValidationCriteria(ValidationCriteria.REQUIRED);

			/* Collection Type Indicator */
			collectionTypeIndicator = new GenericANField(new Position(3, 4),
					"Collection Type Indicator");

			/* routing numbers */
			destinationRoutingNumber = new GenericANField(new Position(5, 13),
					"Destination Routing Number");

			eceInstitutionRoutingNumber = new GenericANField(new Position(14,
					22), "ECE Institution Routing Number");

			/* dates and times - default to now */
			cashLetterBusinessDate = new GenericANField(new Position(23, 30),
					"Cash Letter Business Date");

			cashLetterCreationDate = new GenericANField(new Position(31, 38),
					"Cash Letter Creation Date");

			cashLetterCreationTime = new GenericANField(new Position(39, 42),
					"Cash Letter Creation Time");

			/* record type indicator */
			cashLetterRecordTypeIndicator = new GenericANField(new Position(43,
					43), "Cash Letter Record Type Indicator");

			/* documentation indicator */
			cashLetterDocumentationTypeIndicator = new GenericANField(
					new Position(44, 44),
					"Cash Letter Documentation Type Indicator");

			/* letter ID */
			cashLetterID = new GenericANField(new Position(45, 52),
					"Cash Letter ID");
			cashLetterID.setUsage(Usage.CONDITIONAL);

			/* contact info */
			originatorContactName = new GenericANField(new Position(53, 66),
					"Originator Contact Name");
			originatorContactName.setUsage(Usage.CONDITIONAL);

			originatorContactPhoneNumber = new GenericANField(new Position(67,
					76), "Originator Contact Phone Number");
			originatorContactPhoneNumber.setUsage(Usage.CONDITIONAL);

			/*
			 * fed work type, only used under clearing arrangements, thus no
			 * validator
			 */
			fedWorkType = new GenericANField(new Position(77, 77),
					"Fed Work Type");
			fedWorkType.setUsage(Usage.CONDITIONAL);

			/* user field has no validator */
			returnInd = new GenericANField(new Position(78, 78),
					"Return Indicator");

			/* user field has no validator */
			userField = new GenericANField(new Position(79, 79), "User Field");
			userField.setUsage(Usage.CONDITIONAL);
			/* reserved */
			reserved = new GenericANField(new Position(80, 80), null, " ",
					"Reserved");

			reservedPS = new GenericANField(new Position(81, 90), null, " ",
					"Reserved, ProfitStar UF");

			fields.add(recordType);
			fields.add(collectionTypeIndicator);
			fields.add(destinationRoutingNumber);
			fields.add(eceInstitutionRoutingNumber);
			fields.add(cashLetterBusinessDate);
			fields.add(cashLetterCreationDate);
			fields.add(cashLetterCreationTime);
			fields.add(cashLetterRecordTypeIndicator);
			fields.add(cashLetterDocumentationTypeIndicator);
			fields.add(cashLetterID);
			fields.add(originatorContactName);
			fields.add(originatorContactPhoneNumber);
			fields.add(fedWorkType);
			fields.add(returnInd);
			fields.add(userField);
			fields.add(reserved);
			fields.add(reservedPS);
		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}

	/**
	 * Inits the fields.
	 */
	private void initFields3() {
		try {
			/* cash header record */
			recordType = new GenericANField(new Position(1, 2),
					RecordTypeValidator.INSTANCE, "10", "Record Type");
			recordType.setValidationCriteria(ValidationCriteria.REQUIRED);

			/* Collection Type Indicator */
			collectionTypeIndicator = new GenericANField(new Position(3, 4),
					"Collection Type Indicator");
			collectionTypeIndicator.setFieldData("");

			/* routing numbers */
			destinationRoutingNumber = new GenericANField(new Position(5, 13),
					"Destination Routing Number");
			destinationRoutingNumber.setFieldData("");

			eceInstitutionRoutingNumber = new GenericANField(new Position(14,
					22), "ECE Institution Routing Number");
			eceInstitutionRoutingNumber.setFieldData("");

			/* dates and times - default to now */
			cashLetterBusinessDate = new GenericANField(new Position(23, 30),
					"Cash Letter Business Date");
			cashLetterBusinessDate.setFieldData(EasternStandardTime
					.getCurrentDateInEST());

			cashLetterCreationDate = new GenericANField(new Position(31, 38),
					"Cash Letter Creation Date");
			cashLetterCreationDate.setFieldData(EasternStandardTime
					.getCurrentDateInEST());

			cashLetterCreationTime = new GenericANField(new Position(39, 42),
					"Cash Letter Creation Time");
			cashLetterCreationTime.setFieldData(EasternStandardTime
					.getCurrentTimeInEST());

			/* record type indicator */
			cashLetterRecordTypeIndicator = new GenericANField(new Position(43,
					43), "Cash Letter Record Type Indicator");
			cashLetterRecordTypeIndicator.setFieldData("");

			/* documentation indicator */
			cashLetterDocumentationTypeIndicator = new GenericANField(
					new Position(44, 44),
					"Cash Letter Documentation Type Indicator");
			cashLetterDocumentationTypeIndicator.setFieldData("");

			/* letter ID */
			cashLetterID = new GenericANField(new Position(45, 52),
					"Cash Letter ID");
			cashLetterID.setUsage(Usage.CONDITIONAL);
			cashLetterID.setFieldData("");

			/* contact info */
			originatorContactName = new GenericANField(new Position(53, 66),
					"Originator Contact Name");
			originatorContactName.setUsage(Usage.CONDITIONAL);
			originatorContactName.setFieldData("");

			originatorContactPhoneNumber = new GenericANField(new Position(67,
					76), "Originator Contact Phone Number");
			originatorContactPhoneNumber.setUsage(Usage.CONDITIONAL);
			originatorContactPhoneNumber.setFieldData("");

			/*
			 * fed work type, only used under clearing arrangements, thus no
			 * validator
			 */
			fedWorkType = new GenericANField(new Position(77, 77),
					"Fed Work Type");
			fedWorkType.setUsage(Usage.CONDITIONAL);
			fedWorkType.setFieldData("");

			/* user field has no validator */
			returnInd = new GenericANField(new Position(78, 78),
					"Return Indicator");
			returnInd.setFieldData("");

			/* user field has no validator */
			userField = new GenericANField(new Position(79, 79), "User Field");
			userField.setUsage(Usage.CONDITIONAL);
			userField.setFieldData("");

			/* reserved */
			reserved = new GenericANField(new Position(80, 80), null, " ",
					"Reserved");
			reserved.setFieldData("");

			reservedPS = new GenericANField(new Position(81, 90), null, " ",
					"Reserved, ProfitStar UF");
			reservedPS.setFieldData("");

			fields.add(recordType);
			fields.add(collectionTypeIndicator);
			fields.add(destinationRoutingNumber);
			fields.add(eceInstitutionRoutingNumber);
			fields.add(cashLetterBusinessDate);
			fields.add(cashLetterCreationDate);
			fields.add(cashLetterCreationTime);
			fields.add(cashLetterRecordTypeIndicator);
			fields.add(cashLetterDocumentationTypeIndicator);
			fields.add(cashLetterID);
			fields.add(originatorContactName);
			fields.add(originatorContactPhoneNumber);
			fields.add(fedWorkType);
			fields.add(returnInd);
			fields.add(userField);
			fields.add(reserved);
			fields.add(reservedPS);
		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}

	/**
	 * Inits the fields.
	 */
	private void initFields() {
		try {
			/* cash header record */
			recordType = new GenericNField(new Position(1, 2),
					RecordTypeValidator.INSTANCE, "10", "Record Type");
			recordType.setValidationCriteria(ValidationCriteria.REQUIRED);

			/* Collection Type Indicator */
			collectionTypeIndicator = new GenericNField(new Position(3, 4),
					"Collection Type Indicator");
			collectionTypeIndicator
					.setFieldValidator(CollectionTypeIndicatorValidator.INSTANCE);
			collectionTypeIndicator
					.setValidationCriteria(ValidationCriteria.REQUIRED);

			/* routing numbers */
			destinationRoutingNumber = new GenericNField(new Position(5, 13),
					"Destination Routing Number");
			destinationRoutingNumber
					.setFieldValidator(RoutingNumberValidator.INSTANCE);
			destinationRoutingNumber
					.setValidationCriteria(ValidationCriteria.REQUIRED_IF_PRESENT);

			eceInstitutionRoutingNumber = new GenericNField(
					new Position(14, 22), "ECE Institution Routing Number");
			eceInstitutionRoutingNumber
					.setFieldValidator(RoutingNumberValidator.INSTANCE);
			eceInstitutionRoutingNumber
					.setValidationCriteria(ValidationCriteria.REQUIRED);

			/* dates and times - default to now */
			cashLetterBusinessDate = new GenericNField(new Position(23, 30),
					"Cash Letter Business Date");
			cashLetterBusinessDate.setFieldData(EasternStandardTime
					.getCurrentDateInEST());

			cashLetterCreationDate = new GenericNField(new Position(31, 38),
					"Cash Letter Creation Date");
			cashLetterCreationDate.setFieldData(EasternStandardTime
					.getCurrentDateInEST());

			cashLetterCreationTime = new GenericNField(new Position(39, 42),
					"Cash Letter Creation Time");
			cashLetterCreationTime.setFieldData(EasternStandardTime
					.getCurrentTimeInEST());

			/* record type indicator */
			cashLetterRecordTypeIndicator = new GenericAField(new Position(43,
					43), "Cash Letter Record Type Indicator");
			cashLetterRecordTypeIndicator
					.setFieldValidator(CashLetterRecordTypeValidator.INSTANCE);
			cashLetterRecordTypeIndicator
					.setValidationCriteria(ValidationCriteria.REQUIRED);

			/* documentation indicator */
			cashLetterDocumentationTypeIndicator = new GenericANField(
					new Position(44, 44),
					"Cash Letter Documentation Type Indicator");
			cashLetterDocumentationTypeIndicator
					.setFieldValidator(CashLetterDocumentationTypeValidator.INSTANCE);
			cashLetterDocumentationTypeIndicator
					.setValidationCriteria(ValidationCriteria.REQUIRED_IF_PRESENT);
			cashLetterDocumentationTypeIndicator.setUsage(Usage.CONDITIONAL);

			/* letter ID */
			cashLetterID = new GenericANField(new Position(45, 52),
					"Cash Letter ID");
			cashLetterID.setUsage(Usage.CONDITIONAL);

			/* contact info */
			originatorContactName = new GenericANSField(new Position(53, 66),
					"Originator Contact Name");
			originatorContactName.setUsage(Usage.CONDITIONAL);
			originatorContactName.setFieldData("");

			originatorContactPhoneNumber = new GenericANSField(new Position(67,
					76), "Originator Contact Phone Number");
			originatorContactPhoneNumber.setUsage(Usage.CONDITIONAL);
			originatorContactPhoneNumber.setFieldData("");

			/*
			 * fed work type, only used under clearing arrangements, thus no
			 * validator
			 */
			fedWorkType = new GenericANField(new Position(77, 77),
					"Fed Work Type");
			fedWorkType.setUsage(Usage.CONDITIONAL);
			fedWorkType.setFieldData("");

			/* user field has no validator */
			userField = new GenericANSField(new Position(78, 79), "User Field");
			userField.setUsage(Usage.CONDITIONAL);
			userField.setFieldData("");

			/* reserved */
			reserved = new GenericBlankField(new Position(80, 80), null, " ",
					"Reserved");

			fields.add(recordType);
			fields.add(collectionTypeIndicator);
			fields.add(destinationRoutingNumber);
			fields.add(eceInstitutionRoutingNumber);
			fields.add(cashLetterBusinessDate);
			fields.add(cashLetterCreationDate);
			fields.add(cashLetterCreationTime);
			fields.add(cashLetterRecordTypeIndicator);
			fields.add(cashLetterDocumentationTypeIndicator);
			fields.add(cashLetterID);
			fields.add(originatorContactName);
			fields.add(originatorContactPhoneNumber);
			fields.add(fedWorkType);
			fields.add(userField);
			fields.add(reserved);

		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}
}
